<template>
  <div class="app-container">
    <div class="filterDiv">
      <el-input v-model="listQuery.priceGroupName" size="small" placeholder="价格组名称" style="width: 200px;" class="filter-item" />
      <el-input v-model="listQuery.priceGroupCode" size="small" placeholder="价格组代码" style="width: 200px;" class="filter-item" />
      <el-button size="small" type="primary" round @click="handleFilter">查询</el-button>
      <el-button v-permiss="['301080']" style="float:right" size="small" type="primary" round @click="showImport">导入</el-button>
      <el-button v-permiss="['301020']" size="small" type="primary" round style="float:right" @click="handleAdd">新增</el-button>
    </div>
    <div class="contentDiv">
      <el-table :key="tableKey" v-loading="listLoading" :data="tableData" stripe fit highlight-current-row style="width: 100%;" size="small">
        <el-empty slot="empty" :image-size="120" description="暂无数据" />
        <el-table-column label="序号" type="index" width="100" align="center" />
        <el-table-column label="价格组代码">
          <template slot-scope="{row}">
            <span>{{ row.priceGroupCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="价格组名称">
          <template slot-scope="{row}">
            <span>{{ row.priceGroupName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" align="center">
          <template slot-scope="{row}">
            <span>{{ row.isDisable==0?'是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注">
          <template slot-scope="{row}">
            <span>{{ row.remarks }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="260">
          <template slot-scope="{row}">
            <el-button v-permiss="['301070']" type="text" size="small" @click="handleAssign(row)">分配客户</el-button>
            <el-button v-permiss="['301030']" type="text" size="small" @click="handleCompile(row)">编辑</el-button>
            <el-button v-permiss="['301060']" type="text" size="small" @click="handleDel(row.id)">删除</el-button>
            <el-button v-permiss="['301050']" type="text" size="small" @click="updateStatus(row)">{{ row.isDisable==0?'禁用':'解禁' }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :close-on-click-modal="false" :title="dialogStatus=='create'?'新增价格组':'修改价格组'" :visible.sync="dialogVisible" width="440px">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="100px" style="width: 360px; margin-left:20px;">
        <el-form-item label="价格组代码" prop="priceGroupCode">
          <el-input v-model="form.priceGroupCode" placeholder="价格组代码" />
        </el-form-item>
        <el-form-item label="价格组名称" prop="priceGroupName">
          <el-input v-model="form.priceGroupName" placeholder="价格组名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="备注" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isDisable">
          <el-radio v-model="form.isDisable" label="0">是</el-radio>
          <el-radio v-model="form.isDisable" label="1">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus == 'create'?handleCreate():handleModify()">确定</el-button>
      </div>
    </el-dialog>
    <assignCust ref="custTable" :tit="handleObj.priceGroupName + handleObj.priceGroupCode" type="group" :show-modal.sync="showModal" :handle-obj="handleObj" @handleAssign="handleAssignCust" />
    <import-data ref="importRef" type="priceGroup" title="价格组" />
  </div>
</template>
<script>
import { getPriceGroup, savePriceGroup, delPriceGroup, updatePriceGroupDisabled, updatePriceGroupIdByCustIdList } from '@/api/basedata'
import assignCust from '@/components/assignCust'
import importData from '@/views/set/file/importData'
export default {
  name: 'PriceGroup',
  components: { assignCust, importData },
  data() {
    return {
      tableKey: 0,
      tableData: [],
      custList: [],
      handleObj: {},
      selectIdArr: [], // 初始化时被选的数组
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        priceGroupCode: '',
        priceGroupName: ''
      },
      form: {
        priceGroupName: '',
        priceGroupCode: '',
        remarks: '',
        isDisable: '0'
      },
      dialogVisible: false,
      showModal: false,
      dialogStatus: '',
      rules: {
        priceGroupName: [{ required: true, message: '名称不能为空', trigger: 'change' }],
        priceGroupCode: [{ required: true, message: '代码不能为空', trigger: 'change' }],
        isDisable: [{ required: false }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    showImport() {
      this.$refs.importRef.option = {
        show: true
      }
    },
    handleAssign(row) {
      this.handleObj = row
      this.showModal = true
    },
    handleAssignCust(arr) {
      var obj = {
        priceGroupId: this.handleObj.id,
        custIdList: arr
      }
      updatePriceGroupIdByCustIdList(obj).then(res => {
        if (res.data.errorCode == 0) {
          this.$refs.custTable.closeModal()
          this.getList()
          this.$message.success(res.data.msg)
        }
      })
    },
    getList() {
      this.listLoading = true
      getPriceGroup(this.listQuery).then(res => {
        this.listLoading = false
        this.tableData = res.data.data
      })
    },
    handleAdd(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'create'
      this.form.id = ''
      this.form.priceGroupName = ''
      this.form.priceGroupCode = ''
      this.form.isDisable = '0'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCompile(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'update'
      this.form.id = obj.id
      this.form.priceGroupName = obj.priceGroupName
      this.form.priceGroupCode = obj.priceGroupCode
      this.form.isDisable = String(obj.isDisable)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateStatus(data) {
      this.$confirm('确定' + (data.isDisable == 1 ? '解禁？' : '禁用？'), '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.changeAvaiable(data)
      })
    },
    changeAvaiable(data) {
      var obj = { id: data.id, isDisable: data.isDisable == 1 ? 0 : 1 }
      updatePriceGroupDisabled(obj).then(res => {
        if (res.data.errorCode === '0') {
          this.getList()
          this.$message.success(res.data.msg)
        }
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDel(id) {
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delItem(id)
      })
    },
    handleModify() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          savePriceGroup(this.form).then(res => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    handleCreate() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          savePriceGroup(this.form).then(res => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    delItem(id) {
      delPriceGroup(id).then(res => {
        if (res.data.errorCode === '0') {
          this.getList()
          this.dialogVisible = false
          this.$message.success('删除成功')
        }
      })
    }
  }
}
</script>
<style scoped>
.curTit{font-size: 14px;margin-bottom: 10px}
.fileDiv {
  width: 400px;
  height: 60px;
  border: 1px #d0d0d0 solid;
  line-height: 60px;
  text-align: center;
  border-radius: 6px;
}
.filter-item {
  position: relative;
}
/deep/.el-form-item {
  margin-bottom: 18px;
}
</style>
